package com.uuticketweb.ticket.flight.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.uuticketweb.ticket.db.HqlUtils;
import com.uuticketweb.ticket.util.DateHelper;

public class OrderDao extends HibernateDaoSupport {
	private static final Logger logger = Logger.getLogger(OrderDao.class);
	
	public List searchOrder(String tripKeyword, String guestKeyword, String orderNo, Date createdDate) {
		StringBuffer cond = new StringBuffer();
		if(!StringUtils.isBlank(orderNo)) {
			cond.append(" bmorder.orderno=:orderno ");
		}
		if(createdDate != null) {
			HqlUtils.appendAnd(cond);
			cond.append(" (bmorder.createdDate>=:createdDate and bmorder.createdDate<:createDate2) ");
		}
		if(!StringUtils.isBlank(guestKeyword)) {
			HqlUtils.appendAnd(cond);
			cond.append(" (passenger.name=:guestKeyword and passenger.order.id=bmorder.id) ");
		}
		if(!StringUtils.isBlank(tripKeyword)) {
			HqlUtils.appendAnd(cond);
			cond.append(" ( ");
			cond.append(" bmorder.offAirport=:tripKeyword ");
			cond.append(" or bmorder.downAirport=:tripKeyword ");
			cond.append(" or bmorder.transferAirport=:tripKeyword ");
			cond.append(" or ( ");
			cond.append(" (bmorder.offAirport=threeCode.airportThreeCode or bmorder.downAirport=threeCode.airportThreeCode or bmorder.transferAirport=threeCode.airportThreeCode) ");
			cond.append(" and( threeCode.airportChineseName like :tripKeyword or threeCode.airportEnglishName like :enTripWord )");
			cond.append(" ))"); 
		}
		HqlUtils.insertWhere(cond);
		Session session = getSession();
		String hql = "select distinct bmorder from Order as bmorder";
		if(!StringUtils.isBlank(tripKeyword)) {
			hql += ",ThreeCode as threeCode";
		}
		if(!StringUtils.isBlank(guestKeyword)) {
			hql += ", Passenger as passenger";
		}
		Query query = session.createQuery(hql + cond);
		if(!StringUtils.isBlank(orderNo)) {
			query.setString("orderno", orderNo);
		}
		if(createdDate != null) {
			query.setDate("createdDate", createdDate);
			query.setDate("createDate2", DateHelper.addDays(createdDate, 1));
		}
		if(!StringUtils.isBlank(tripKeyword)) {
			query.setString("tripKeyword", tripKeyword);
			query.setString("enTripWord", HqlUtils.getWildcardString(tripKeyword));
		}
		if(!StringUtils.isBlank(guestKeyword)) {
			query.setString("guestKeyword", guestKeyword);
		}
		return query.list();
	}
	
	public List searchSimpleOrder(String orderNo, String tripKeyword, Date createdDate) {
		StringBuffer cond = new StringBuffer();
		if(!StringUtils.isBlank(orderNo)) {
			cond.append(" simpleOrder.orderno=:orderno ");
		}
		if(createdDate != null) {
			HqlUtils.appendAnd(cond);
			cond.append(" (simpleOrder.createdDate>=:createdDate and simpleOrder.createdDate<:createDate2) ");
		}
		if(!StringUtils.isBlank(tripKeyword)) {
			HqlUtils.appendAnd(cond);
			cond.append(" simpleOrder.tripDetail like :tripKeyword");
		}
		HqlUtils.insertWhere(cond);
		Session session = getSession();
		Query query = session.createQuery("from SimpleOrder simpleOrder " + cond);
		if(!StringUtils.isBlank(orderNo)) {
			query.setString("orderno", orderNo);
		}
		if(createdDate != null) {
			query.setDate("createdDate", createdDate);
			query.setDate("createDate2", DateHelper.addDays(createdDate, 1));
		}
		if(!StringUtils.isBlank(tripKeyword)) {
			query.setString("tripKeyword", HqlUtils.getWildcardString(tripKeyword));
		}
		return query.list();
	}
}
